using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CharacterDataManager{
	#region SINGLETON
	private static CharacterDataManager instance = null;
	public static CharacterDataManager GetInstance(){
		if (instance == null)
			instance = new CharacterDataManager();
		return instance;
	}
	#endregion
	public CharacterDataManager(){
		charactersData = new SortedList<int,CharacterData>();	
		CreateNames();
		
		string[] charNames = GetRandomNames();
		charactersData.Add(0,new CharacterData(charNames[0]) );
		charactersData.Add(1,new CharacterData(charNames[1] ) );
		charactersData.Add(2,new CharacterData(charNames[2] ) );
		charactersData.Add(3,new CharacterData(charNames[3] ) );
	}
		private static string dataFileName = "saveData";
	private List<string> namesList;
    
	private SortedList<int, CharacterData> charactersData;
	private int currentIndex = 0;
    bool loadedCharacter = false;
	
	private void CreateNames(){
		namesList = new List<string>();
		namesList.Add("Juan");
		namesList.Add(	"Tito");
		namesList.Add(	"Alberto");
		namesList.Add(	"Truchinni");
		namesList.Add(	"A. Gorila");
		namesList.Add(	"Ivan");
		namesList.Add(	"Cristian");
		namesList.Add(	"Lucho");
		namesList.Add(	"Gonzalo");
		namesList.Add(	"Pedro");
		namesList.Add(	"Horacio");
		namesList.Add(	"Smith");
		namesList.Add(	"Leonardo");
		namesList.Add(	"Gabriel");
		namesList.Add(	"'El Tumba'");
		namesList.Add(	"Pollo");
		namesList.Add(	"El Topo");
	}
	public string[] GetRandomNames(){
		string[] names = new string[4];
		List<string> aux = new List<string>();
		
		for(int i=0; i<4; i++){
			int idx = Random.Range(0,namesList.Count);
			names[i] = namesList[idx];	
			aux.Add(namesList[idx]);
			namesList.Remove(names[i]);
		}
		for (int i=0; i<4; i++){
			namesList.Add(aux[i]);
		}
		return names; 
	}
	public int GetCharactersCount(){ return this.charactersData.Count; }
	public CharacterData GetCurrentCharData(){
		return charactersData[currentIndex];
	}
	public void ResetCharData(){
		string[] names = GetRandomNames();
		for (int i=0; i< charactersData.Count; i++){
			charactersData[i] = new CharacterData(names[i]);	
		}
	}
    /// <summary>
    /// Devuelve el character data q pedis por indice.
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public CharacterData GetCharacterData(int index)
    {	//LA ASIGNACION LA TIENE QUE VERIFICAR EL QUE PIDE EL DATO 
		//SINO NO SE PUEDE CONSULTAR UN CHARACTERDATA ASIGNADO.
    /*    if (!charactersData[index].assigned)
        {
            charactersData[index].assigned = true;
            return charactersData[index];
        }
        else
        {
            Debug.LogWarning("Hijo de mil puta, ese indice no existe o esta usado");
            return null;
        }
    */
		if (charactersData.ContainsKey(index)){
			return charactersData[index];	
		}
		else{
			Debug.LogWarning("Hijo de mil puta, ese indice no existe");
            return null;
		}
    }
	public CharacterData GetNextCharData(){
		currentIndex++;
		if (currentIndex >= charactersData.Count)
			currentIndex = 0;
		return charactersData[currentIndex];	
	}
	public CharacterData GetPreviousCharData(){
		currentIndex--;
		if (currentIndex < 0)
			currentIndex = charactersData.Count-1;
		return charactersData[currentIndex];
	}
	public void SetCharacterData(CharacterData charData){
		charactersData[currentIndex] = charData;	
	}
	public void SetCharacterData(CharacterData charData,int index){
		charactersData[index] = charData;	
	}
	public int GetCurrentIndex(){return this.currentIndex;}
	public void SetCurrentIndex(int index){
		if (index < charactersData.Count){
			currentIndex = index;	
			if (GUIPanelInGame.instance != null)
				GUIPanelInGame.instance.LocateSelectionArrow(currentIndex);
		}
	}
    public void UnAssignAll()
    {
        foreach (KeyValuePair<int, CharacterData> charData in charactersData)
            charData.Value.assigned = false;
    }
	public void SaveCharacters(){
		string[] names = new string[charactersData.Count];
		
		for	(int i=0; i<charactersData.Count; i++){
			charactersData[i].Save();
			Debug.Log("Saved Character: " + charactersData[i].GetName() + ".bin" );
			names[i] = charactersData[i].GetName();
		}
		SavesInfo save = new SavesInfo(names);
		SaveDataManager.GetInstance().SaveFile((object)save, dataFileName);
	}
	public void LoadCharacters(){
        if (loadedCharacter) return;
		SavesInfo saveInfo = 
			SaveDataManager.GetInstance().LoadFile<SavesInfo>(dataFileName);
			Debug.Log("Loaded dataFile OK");
		
		for (int i=0; i<saveInfo.listOfNames.Length; i++){
			CharacterData character = new CharacterData();
			character.Load(saveInfo.listOfNames[i]);
			SetCharacterData(character,i);	
			Debug.Log("Loaded Character: " + character.GetName() + ".bin" );
		}
        loadedCharacter = true;
	}
    /// <summary>
    /// Se llama cuando vuelve al menu.
    /// </summary>
    public void OnEndGame()
    {
        for (int i = 0; i < charactersData.Count; i++)
        {
            charactersData[i].assigned = false;
            charactersData[i].ResetHotSkills();
            charactersData[i].RefillValues();
        }
    }
    public void AddAditionalPoints(int amount)
    {
        for (int i = 0; i < charactersData.Count; i++)
        {
            charactersData[i].attributes.AddAttributePoints(amount);
        }
    }

}
